package com.googlecode.dgwt.client.dojox.data.model;

import com.googlecode.dgwt.client.dojo.Dojo;
import com.googlecode.dgwt.client.dojo.exception.DojoRuntimeException;
import com.googlecode.dgwt.client.dojo.reflect.BeanInfo;
import com.googlecode.dgwt.client.dojo.reflect.BeanMethod;
import com.googlecode.dgwt.client.dojo.reflect.DojoBean;
import com.googlecode.dgwt.client.dojo.reflect.HasBeanInfo;
import com.googlecode.dgwt.client.dojox.data.model.base.DataModelBase;


/**
 * @author      Wellington Tadeu dos Santos
 * @since       DGWT v0.01
 */
public class BeanModel<Item> extends DataModelBase<Item, BeanColumn<Item>> implements HasBeanInfo<Item> {

    
    private final BeanInfo<Item> beanInfo;
    
    
    @SuppressWarnings("unchecked")
    public BeanModel(Class<? extends DojoBean> classType) {
        beanInfo = (BeanInfo<Item>) Dojo.reflect.getBeanInfo(classType);
    }


    public BeanInfo<Item> getBeanInfo() {
        return beanInfo;
    }


    @Override
    public Item createItem() {
        BeanMethod<Item> method = beanInfo.findConstructor();
        if (method == null) {
            throw new DojoRuntimeException(this, "Could not create a new instance of \"" + 
                    beanInfo.getName() + "\". No default constructor was found.");
        }
        return method.call(null);
    }    
    
}
